<?php
/**
 * 异步交互控制器
 * @author jianhui
 *
 */
class Ajax extends MY_Controller{
    function Ajax(){
        parent::MY_Controller();
        require_once APPPATH.'libraries/JSON.php';
    }
    /**
     * 载入年份列表
     */
    function loadYearList(){
        //时间单位配置
        $this->load->model('timeUnitModel');
        $timeUnitList = $this->timeUnitModel->getCacheData('yearList');
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        foreach ($timeUnitList as $k => $v){
            $list[] = array('key'=>$k, 'value'=>$v);
        }
        $json = new Services_JSON();
        echo $json->encode($list);
        exit();
    }
    /**
     * 载入时段列表
     */
    function loadTimeUnitList(){
        $year = $this->input->post('year');
        $year || $year = date('Y');
        //时间单位配置
        $this->load->model('timeUnitModel');
        $timeUnitList = $this->timeUnitModel->getTimeUnitList($year);
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        foreach ($timeUnitList as $k => $v){
            $list[] = array('key'=>$k, 'value'=>$v);
        }
        $json = new Services_JSON();
        echo $json->encode($list);
        exit();
    }
    /**
     * 载入活动类型列表
     */
    function loadEventType(){
        $eventType = $this->config->item('projectEventType');
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        if(isset($eventType)){
            foreach ($eventType as $k => $v){
                $list[] = array('key'=>$k, 'value'=>$v);
            }
        }
        $json = new Services_JSON();
        echo $json->encode($list);
        exit();
    }
    /**
     * 载入对应活动类型的活动分类列表
     */
    function loadProjectEventClass(){
        $eventType = (int)$this->input->post('eventType');
        $eventClass = $this->config->item('projectEventClass');
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        if(isset($eventClass[$eventType])){
            foreach ($eventClass[$eventType] as $k => $v){
                $list[] = array('key'=>$k, 'value'=>$v);
            }
        }
        $json = new Services_JSON();
        echo $json->encode($list);
        exit();
    }
    /**
     * 载入一级媒体列表
     */
    function loadGxMediaType1(){
        $mediaType1List = $this->config->item('gxMediaType1');
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        if(isset($mediaType1List)){
            foreach ($mediaType1List as $k => $v){
                $list[] = array('key'=>$k, 'value'=>$v);
            }
        }
        $json = new Services_JSON();
        echo $json->encode($list);
        exit();
    }
    /**
     * 载入对应二级媒体列表
     */
    function loadGxMediaType2(){
        $mediaType1 = (int)$this->input->post('mediaType1');
        $mediaType2List = $this->config->item('gxMediaType2');
        $list = array();
        if (isset($mediaType2List[$mediaType1])){
            $list[] = array('key'=>'','value'=>'请选择');
            foreach ($mediaType2List[$mediaType1] as $k => $v){
                $list[] = array('key'=>$k, 'value'=>$v);
            }
        }
        $json = new Services_JSON();
        echo $json->encode($list);
        exit();
    }
    /**
     * 载入常规媒体媒体尺寸列表
     */
    function loadGxMediaType21Size(){
        $mediaType1 = (int)$this->input->post('mediaType1');
        $mediaType2 = (int)$this->input->post('mediaType2');
        $gxMediaType21Size = $this->config->item('gxMediaType21Size');
        $list = array();
        if ($mediaType1 == 1 && isset($gxMediaType21Size[$mediaType2])){
            $list[] = array('key'=>'','value'=>'请选择');
            foreach ($gxMediaType21Size[$mediaType2] as $k => $v){
                $list[] = array('key'=>$k, 'value'=>$v);
            }
        }
        $json = new Services_JSON();
        echo $json->encode($list);
        exit();
    }
    /**
     * 载入线下活动-明细项类型
     */
    function loadOfflineDetailType(){
        $offlineDetailType = $this->config->item('offlineDetailType');
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        if (isset($offlineDetailType)){
            foreach ($offlineDetailType as $k => $v){
                $list[] = array('key'=>$k, 'value'=>$v);
            }
        }
        $json = new Services_JSON();
        echo $json->encode($list);
        exit();
    }
    /**
     * 载入线下活动费用项目描述
     */
    function loadOfflineDetailDescType(){
        $type = $this->input->post('type');
        $offlineDetailDescType = $this->config->item('offlineDetailDescType');
        $list = array();
        $list[] = array('key'=>'','value'=>'请选择');
        if (isset($offlineDetailDescType[$type])){
            foreach ($offlineDetailDescType[$type] as $k => $v){
                $list[] = array('key'=>$k, 'value'=>$v);
            }
        }
        $json = new Services_JSON();
        echo $json->encode($list);
        exit();
    }
    /**
     * 载入地区列表
     */
    function loadPlaceList(){
        $type = $this->input->post('type');
        $lastLevelValue = $this->input->post('lastLevelValue');
        $list = array();
        switch ($type){
            case 'area':
                $list = $this->userLib->getAreaList();
                array_unshift($list, array('key'=>'', 'value'=>'请选择地区'));
                break;
            case 'region':
                $lastLevelValue && $list = $this->userLib->getRegionList($lastLevelValue);
                array_unshift($list, array('key'=>'', 'value'=>'请选择大区'));
                break;
            case 'province':
                $lastLevelValue && $list = $this->userLib->getProvinceList($lastLevelValue);
                array_unshift($list, array('key'=>'', 'value'=>'请选择省份'));
                break;
            case 'city':
                $lastLevelValue && $list = $this->userLib->getCityList($lastLevelValue);
                array_unshift($list, array('key'=>'', 'value'=>'请选择城市'));
                break;
            case 'store':
                $lastLevelValue && $list = $this->userLib->getStoreList($lastLevelValue);
                array_unshift($list, array('key'=>'', 'value'=>'请选择专营店'));
                break;
        }
        $json = new Services_JSON();
        echo $json->encode($list);
        exit();
    }
    /**
     * 搜索专营店列表
     */
    function searchStore(){
        //专营店筛选
        $area = $this->input->post('area');
        $region = $this->input->post('region');
        $spsarea = $this->input->post('spsarea');
        $province = $this->input->post('province');
        $city = $this->input->post('city');
        $keyword = $this->input->post('keyword');
        //权限内专营店列表
        $storeId = 0;
        $viewStoreIds = $this->userLib->getViewStoreIds();
        $viewStoreIds != '*' && $storeId = $viewStoreIds; 
        //查找对应地区列表
        $this->load->model('sellpointModel');
        $searchStoreList = $this->sellpointModel->searchStore($area, $region, $spsarea, $province, $city, $storeId, $keyword);
        $list = array();
        foreach($searchStoreList as $v){
            $list[] = array('key'=>(int)$v['userId'], 'value'=>$v['storeName']);
        }
        $json = new Services_JSON();
        echo $json->encode($list);
        exit();
    }
}